package com.heima.schedule.feign;

import com.heima.feign.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.schedule.dtos.TaskDto;
import com.heima.schedule.service.TaskinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jack
 * @data 2024 16:05
 */
@RestController
public class ScheduleClient implements IScheduleClient {
    @Autowired
    private TaskinfoService taskinfoService;

    /**
     * 添加任务
     *
     * @param taskDto
     */
    @PostMapping("/api/v1/schedule/addTask")
    public void addTask(@RequestBody TaskDto taskDto){
        taskinfoService.addTask(taskDto);
    };

    /**
     * 拉取任务
     *
     * @param taskType 任务类型
     * @param priority 任务优先级
     * @return
     */
    @PostMapping("/api/v1/schedule/pullTask/{taskType}/{priority}")
    public ResponseResult pullTask(@PathVariable Integer taskType,
                                   @PathVariable Integer priority){
        TaskDto taskDto = taskinfoService.pullTask(taskType, priority);
        if (taskDto!= null) {
            byte[] parameters = taskDto.getParameters();
            String newsIdStr = new String(parameters);

            return ResponseResult.okResult(newsIdStr);
        }else {
            return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    };
}
